/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2015 Umeng, Inc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.umeng.comm.ui.imagepicker.activities;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.comm.core.beans.CommConfig;
import com.umeng.comm.core.beans.MessageCount;
import com.umeng.comm.core.utils.ResFinder;
import com.umeng.comm.ui.activities.NewMsgActivity;
import com.umeng.comm.ui.fragments.NewMsgFragments;
import com.umeng.comm.ui.imagepicker.fragments.CommentTabFragment;
import com.umeng.community.example.MainActivity;

public abstract class NewMsgBaseActivity extends BaseFragmentActivity implements OnClickListener {

    public MessageCount mUnreadMsg = CommConfig.getConfig().mMessageCount;

    private TextView mTitleView;
    //将返回键按钮定义
    private ImageButton mBackView;

    public View[] mDots;
    public Fragment[] mFragments;

    public View mFragmentContainerView;
    public View mBtnContainerView;

    //回复的头部不显示名字而显示发出和收到的评论
//    private LinearLayout mComment;
//    private TextView mBtnOne;
//    private TextView mBtnTwo;

    private RelativeLayout mHeader;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(ResFinder.getLayout("umeng_commm_my_msg_layout"));
        setFragmentContainerId(ResFinder.getId("umeng_comm_my_msg_fragment"));


        initParams();
        initTitle();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // message session unread count
        updateDotsVisiable(3);
    }

    public abstract void initParams();

    public abstract void settingBtnViews();

    public abstract void onBtnViewClick(int id);

    /**
     * 初始化title</br>
     *
     * @param context
     */
    private void initTitle() {
        int backButtonResId = ResFinder.getId("umeng_comm_title_back_btn");
        mBackView = (ImageButton) findViewById(backButtonResId);
        mBackView.setOnClickListener(this);
        mBackView.setVisibility(View.GONE);

        int titleResId = ResFinder.getId("umeng_comm_title_tv");
        mTitleView = (TextView) findViewById(titleResId);
        mTitleView.setText("消息");

        int headerResId = ResFinder.getId("comment_header");
        mHeader = (RelativeLayout) findViewById(headerResId);

//        int layResId = ResFinder.getId("uemng_switch_button_comment");
//        mComment = (LinearLayout) findViewById(layResId);
//        mComment.setVisibility(View.GONE);
//        int btnOneResId = ResFinder.getId("comment_switch_button_one");
//        mBtnOne = (TextView) findViewById(btnOneResId);
//        mBtnOne.setOnClickListener(this);
//        int btnTwoResId = ResFinder.getId("comment_switch_button_two");
//        mBtnTwo = (TextView) findViewById(btnTwoResId);
//        mBtnTwo.setOnClickListener(this);

        mFragmentContainerView = findViewById(ResFinder.getId("umeng_comm_my_msg_fragment"));
        mFragmentContainerView.setVisibility(View.GONE);
        mBtnContainerView = findViewById(ResFinder.getId("umeng_comm_my_msg_item_content"));

        settingBtnViews();
    }

    public void initBtnView(int viewResId, final String btnStr, String iconResStr, final int id,
                            boolean hasDivideLine) {
        View v = findViewById(viewResId);
        v.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                onBtnViewClick(id);
                updateUnreadMsgCount(id);
                updateDotsVisiable(id);
                mTitleView.setText(btnStr);
            }
        });

        TextView tv = (TextView) v.findViewById(ResFinder.getId("umeng_comm_my_msg_text_view"));
        tv.setText(btnStr);

        ImageView iconImg = (ImageView) v.findViewById(ResFinder.getId("umeng_comm_my_msg_icon_view"));
        iconImg.setImageDrawable(ResFinder.getDrawable(iconResStr));

        mDots[id] = v.findViewById(ResFinder.getId("umeng_comm_my_msg_badge_view"));
        updateDotsVisiable(id);

        if (!hasDivideLine) {
            v.findViewById(ResFinder.getId("umeng_comm_divide_line")).setVisibility(View.GONE);
        }
    }

    public void setData(String name, int id) {
        if (id != 0) {
            mTitleView.setVisibility(View.VISIBLE);
//            mComment.setVisibility(View.GONE);
            mTitleView.setText(name);
            mBackView.setVisibility(View.VISIBLE);
        } else {
            mTitleView.setVisibility(View.GONE);
//            mComment.setVisibility(View.VISIBLE);
//            mBtnOne.setSelected(true);
            mHeader.setVisibility(View.GONE);
            mBackView.setVisibility(View.VISIBLE);
        }
    }

    public void updateDotsVisiable(int position) {
        int unReadMsgCount = 0;
        if (mUnreadMsg != null) {
            switch (position) {
                case 0:
                    unReadMsgCount = mUnreadMsg.unReadCommentsCount;
                    break;

                case 1:
                    unReadMsgCount = mUnreadMsg.unReadLikesCount;
                    break;

                case 2:
                    unReadMsgCount = mUnreadMsg.unReadAtCount;
                    break;

                case 3:
                    unReadMsgCount = mUnreadMsg.unReadNotice;
                    break;

//                case 3:
//                    unReadMsgCount = mUnreadMsg.unReadSessionsCount;
//                    break;


                default:
                    break;
            }
        }
//        mDots[position].setVisibility(unReadMsgCount > 0 ? View.VISIBLE : View.GONE);
    }

    public void updateUnreadMsgCount(int index) {
        switch (index) {
            case 0:
                mUnreadMsg.unReadCommentsCount = 0;
                break;

            case 1:
                mUnreadMsg.unReadLikesCount = 0;
                break;

            case 2:
                mUnreadMsg.unReadAtCount = 0;
                break;

            case 3:
                mUnreadMsg.unReadNotice = 0;
                break;

//            case 3:
//
//                break;


            default:
                break;
        }
        mUnreadMsg.unReadTotal = mUnreadMsg.unReadAtCount + mUnreadMsg.unReadCommentsCount
                + mUnreadMsg.unReadLikesCount + mUnreadMsg.unReadNotice +
                mUnreadMsg.newFansCount + mUnreadMsg.unReadSessionsCount;
    }


    @Override
    public void onClick(View v) {
        if (!hideFragmentContainerView()) {
//            finish();
        }
//        switch (v.getId()) {
//            case R.id.umeng_comm_title_back_btn:
//                if (!hideFragmentContainerView()) {
//                    finish();
//                }
//                break;
//            case R.id.comment_switch_button_one:
//                mBtnOne.setSelected(true);
//                mBtnTwo.setSelected(false);
//                CommentTabFragment.tab = 0;
//                break;
//            case R.id.comment_switch_button_two:
//                mBtnOne.setSelected(false);
//                mBtnTwo.setSelected(true);
//                CommentTabFragment.tab = 1;
//                break;
//            default:
//                break;
//        }
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK && hideFragmentContainerView()) {
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//
//    }

    private boolean hideFragmentContainerView() {
        if (mFragmentContainerView.getVisibility() == View.VISIBLE) {
            mFragmentContainerView.setVisibility(View.GONE);
//            mBtnContainerView.setVisibility(View.VISIBLE);
//            mTitleView.setText("消息");
//            MainActivity.actionStart(this, true);
            finish();
            return true;
        } else {
            return false;
        }
    }
}
